'use strict'

//const pool = require('../pool.js');

class getarticles{
  constructor(){
    this.param = '/'
  }

  async get(c){
    let a = c.service.fun;
    let arr = [];
    let {user_id} = c.query;
    console.log(user_id);
    c.setHeader('content-type', 'text/html; charset=utf-8')
    
    let offset = c.query.offset || 0

    let articleList = await c.service.pqorm.model('article_list a')
                                      .leftJoin('user_list u', 'a.poster_id = u.user_id')
                                      .limit(20, offset)
                                      .select("article_id,article_title,article_txt,article_img,article_time,article_place,poster_id,user_name,user_head");

    let data1= await a.sel('like_list','',[],c.service.pool);
    let aa =data1.rows.some(item=>{
      return user_id == item.like_user_id
    })
    if(aa){
      let line = await a.sel('like_list',{'like_user_id':`${user_id}`},['liked_article_id'],c.service.pool);
      for(let i=0;i<articleList.rows.length;i++){
        let liked_article_id2 = articleList.rows[i].article_id;
        for(let j=0;j<line.rows.length;j++){
          let {liked_article_id} = line.rows[j];
          if(liked_article_id2 === liked_article_id){
            articleList.rows[i].iflike = true;
            console.log(liked_article_id2);
            break;
          }else{
            articleList.rows[i].iflike = false;
          }
        }
      }
      c.send({
        type : 'array',
        data : articleList.rows
      })
    }else{
      for(let i=0;i<articleList.rows.length;i++){
        articleList.rows[i].iflike = false;
      }
      c.send({
        type : 'array',
        data : articleList.rows
      })
    }
    // let line = await a.sel('like_list',{'like_user_id':`${user_id}`},['liked_article_id'],c.service.pool);
    // //console.log(line.rows);
    // for(let i=0;i<articleList.rows.length;i++){
    //   let liked_article_id2 = articleList.rows[i].article_id;
    //   for(let j=0;j<line.rows.length;j++){
    //     let {liked_article_id} = line.rows[j];
    //     if(liked_article_id2 === liked_article_id){
    //       articleList.rows[i].iflike = true;
    //       console.log(liked_article_id2);
    //       break;
    //     }else{
    //       articleList.rows[i].iflike = false;
    //     }

    //   }
    // }
    
  }
}
module.exports = getarticles;
